package com.mobi.cacti.ns.view.adapter;

import java.util.ArrayList;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Activity;
import org.holoeverywhere.widget.ArrayAdapter;
import org.holoeverywhere.widget.TextView;

import com.mobi.cacti.R;
import com.mobi.cacti.ns.model.ListItem;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ItemListAdapter extends ArrayAdapter<ListItem> {

	private Activity context;
	private ArrayList<ListItem> list;

	static class ViewHolder {
		protected TextView label;
		protected TextView ticketCountLabel;
		protected RelativeLayout ticketCountCircle;
		protected ImageView rightArrow;
	}

	public ItemListAdapter(Activity context, int textViewResourceId,
			ArrayList<ListItem> objects) {
		super(context, textViewResourceId, objects);

		this.context = context;
		this.list = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;

		if (convertView == null) {
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.rowitem_layout, null);
			
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.label = (TextView) view.findViewById(R.id.itemlabel);
			viewHolder.ticketCountLabel = (TextView) view.findViewById(R.id.ticketcounterlabel);
			viewHolder.ticketCountCircle = (RelativeLayout) view.findViewById(R.id.ticketcounter);
			viewHolder.rightArrow = (ImageView) view.findViewById(R.id.rightarrow);
			
			view.setTag(viewHolder);
		} else {
			view = convertView;
		}
		
		ListItem item = list.get(position);
		
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.label.setText(item.label);
		
		if (item.ticketCountLabel == null) {
			holder.ticketCountCircle.setVisibility(View.GONE);
			holder.ticketCountLabel.setVisibility(View.GONE);
		} else {
			holder.ticketCountCircle.setVisibility(View.VISIBLE);
			holder.ticketCountLabel.setVisibility(View.VISIBLE);
			holder.ticketCountLabel.setText(item.ticketCountLabel);
		}
		
		if (!item.isShowRightArrow) {
			holder.rightArrow.setVisibility(View.GONE);
		} else {
			holder.rightArrow.setVisibility(View.VISIBLE);
		}
		
		return view;
	}
}
